import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisStringDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.flushAll();
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }

    private static void test4(Jedis jedis) {
        System.out.println("append");

        jedis.set("k1", "1234");
        jedis.append("k1", "567890");
        System.out.println("result: " + jedis.get("k1"));
    }

    private static void test3(Jedis jedis) {
        System.out.println("incr and decr");

        jedis.set("k1", "100");
        long result = jedis.incr("k1");
        System.out.println("result: " + result);
        String k1 = jedis.get("k1");
        System.out.println("k1: " + k1);

        result = jedis.decr("k1");
        System.out.println("result: " + result);
        k1 = jedis.get("k1");
        System.out.println("k1: " + k1);
    }

    private static void test2(Jedis jedis) {
        System.out.println("getrange and setrange");

        jedis.set("k1", "1234567890");
        String value = jedis.getrange("k1", 2, -2);
        System.out.println("value: " + value);

        jedis.setrange("k1", 2, "7758258111111111");
        value = jedis.getrange("k1", 0, -1);
        System.out.println("value: " + value);

    }

    private static void test1(Jedis jedis) {
        System.out.println("mset and mget");

        String result = jedis.mset("k1", "111", "k2", "222", "k3", "333");
        System.out.println("result: " + result);

        List<String> values = jedis.mget("k1", "k2", "k30", "k3");
        System.out.println(values);
    }
}
